﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_INV_RETURN_IMPORT_ITEM
// * CreateDate    : 2013/1/16 20:30:43
// * Creater       : InvReturnImportItemBs
// * LastChangeDate: 2013/1/16 20:30:43
// * LastChanger   : 2013/1/16 20:30:43
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Inv
{
    /// <summary>
    /// InvReturnImportItem
    /// </summary>
    public partial class InvReturnImportItemBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objInvReturnImportItemEntity">Entity</param>
        /// <returns>Entity</returns>
        public static InvReturnImportItemEntity GetInvReturnImportItem(InvReturnImportItemEntity objInvReturnImportItemEntity)
        {
            return DataAccess.SelectSingle<InvReturnImportItemEntity>(objInvReturnImportItemEntity);
        }
        public static DataTable GetInvReturnImportItemByOrderId(string OrderId)
        {
            string sql = @"SELECT A.*,B.PRODUCT_NAME,B.PRODUCT_CODE,B.PRODUCT_SPEC_OUT,B.UNIT,C.WAREHOUSE_NAME,C.WAREHOUSE_CODE,D.LOCATION_CODE FROM dbo.INV_RETURN_IMPORT_ITEM A 
                        LEFT JOIN MST_PRODUCT B ON A.PRODUCT_ID=B.PRODUCT_ID
                        LEFT JOIN dbo.MST_WAREHOUSE C ON A.MST_WAREHOUSE_ID=C.WAREHOUSE_CODE
                        LEFT JOIN dbo.MST_LOCATION D ON A.MST_LOCATION_ID=D.LOCATION_ID
                        WHERE a.INV_RETURN_IMPORT_ORDER_ID='" + OrderId + "'";
            DataTable dt = DataAccess.SelectDataSet(sql).Tables[0];
            return dt;
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objInvReturnImportItemEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertInvReturnImportItem(InvReturnImportItemEntity objInvReturnImportItemEntity)
        {
            return DataAccess.Insert(objInvReturnImportItemEntity);
        }

        public static bool InsertInvReturnImportItem(List<InvReturnImportItemEntity> objInvReturnImportItemEntityList)
        {
            return DataAccess.Insert(objInvReturnImportItemEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objInvReturnImportItemEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateInvReturnImportItem(InvReturnImportItemEntity objInvReturnImportItemEntity)
        {
            return DataAccess.Update(objInvReturnImportItemEntity);
        }

        public static bool UpdateInvReturnImportItem(List<InvReturnImportItemEntity> objInvReturnImportItemEntityList)
        {
            return DataAccess.Update<InvReturnImportItemEntity>(objInvReturnImportItemEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objInvReturnImportItemEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteInvReturnImportItem(InvReturnImportItemEntity objInvReturnImportItemEntity)
        {
            return DataAccess.Delete(objInvReturnImportItemEntity);
        }

        public static bool DeleteInvReturnImportItem(List<InvReturnImportItemEntity> objInvReturnImportItemEntityList)
        {
            return DataAccess.Delete<InvReturnImportItemEntity>(objInvReturnImportItemEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetInvReturnImportItemAll()
        {
            return DataAccess.SelectAllDataSet<InvReturnImportItemEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<InvReturnImportItemEntity> GetInvReturnImportItemAllList()
        {
            return DataAccess.SelectAll<InvReturnImportItemEntity>();
        }
        #endregion

        public static decimal GetRemindQtyBySalesReturnItemId(string ReturnItemId)
        {
            decimal remind_qty = 0;
            string sql = @"select sum(CONVERT(DECIMAL(18,2),IMPORT_QTY)) from dbo.INV_RETURN_IMPORT_ITEM where SALES_RETURN_ITEM_ID='" + ReturnItemId + "' GROUP BY SALES_RETURN_ITEM_ID";
            DataSet ds = DataAccess.SelectDataSet(sql);
            SalesReturnItemEntity entity = new SalesReturnItemEntity();
            entity.Id = ReturnItemId;
            SalesReturnItemEntity ReturnItem = DataAccess.SelectSingle<SalesReturnItemEntity>(entity);


            if (ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
            {
                remind_qty = Convert.ToDecimal(ReturnItem.ReturnQty);
                return remind_qty;
            }
            remind_qty = Convert.ToDecimal(ReturnItem.ReturnQty)-Convert.ToDecimal(ds.Tables[0].Rows[0][0].ToString());

            return remind_qty;
        }
    }


}

